package com.gitee.loyo.spring

import com.gitee.loyo.act.TUserModel
import com.gitee.loyo.act.User
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional


@Service
open class TestService{
    @Autowired lateinit var mTestTransactionService: TestTransactionService

    open fun createUser(): User{
        val user = User(userId = 1L, userName = "admin", sex = 1, active = false)
        TUserModel.add(user)
        return user
    }

    open fun tryCreateUsers(){
        TUserModel.add(User(userId = 3L, userName = "admin", sex = 1, active = false))
        mTestTransactionService.failToCreateUsers()
        TUserModel.add(User(userId = 4L, userName = "guest", sex = 1, active = false))
    }

    @Transactional
    open fun createUsersWithNest(){
        mTestTransactionService.nestTranCreateUsers()
        TUserModel.add(User(userId = 3L, userName = "admin", sex = 1, active = false))
        TUserModel.add(User(userId = 4L, userName = "guest", sex = 1, active = false))
        throw RuntimeException()
    }

    @Transactional
    open fun createUsersWithNever(){
        TUserModel.add(User(userId = 3L, userName = "admin", sex = 1, active = false))
        TUserModel.add(User(userId = 4L, userName = "guest", sex = 1, active = false))
        mTestTransactionService.neverTranCreateUsers()
    }

    @Transactional
    open fun createUsersWithNew(){
        mTestTransactionService.newTranCreateUsers()
        TUserModel.add(User(userId = 3L, userName = "admin", sex = 1, active = false))
        TUserModel.add(User(userId = 4L, userName = "guest", sex = 1, active = false))
        throw RuntimeException()
    }
}